package com.menker.app.biz;

import com.menker.app.Constants;
import com.menker.app.common.SharedPreferencesHelper;
import com.menker.app.rest.result.AppInfo;
import com.menker.app.rest.result.ProductShareInfo;


/**
 * @author J.Huang
 * @version 1.0
 */

public class CurrentUser {

    private final SharedPreferencesHelper mSharedPrefHelper;

    private AppInfo mAppInfo = null;

    private ProductShareInfo mShareInfo = null;

    public CurrentUser(SharedPreferencesHelper sharedPrefHelper) {
        mSharedPrefHelper = sharedPrefHelper;
    }

    public void setAppInfo(AppInfo appInfo) {
        mAppInfo = appInfo;
    }

    public boolean hasLoggedIn() {
        if (mAppInfo != null) {
            if ("login".equals(mAppInfo.UserStatus.toLowerCase())) {
//                if (!TextUtils.isEmpty(mAppInfo.CookieName)) {
//                    mSharedPrefHelper.setData(Constants.API.KEY_TICKET, mAppInfo.CookieName);
//                }
                return true;
            }
        }

        return false;
    }

    public int getLatestAppVersion() {
        return mAppInfo != null ? mAppInfo.AppVersion : 0;
    }

    public String getAppDownloadUrl() {
        return mAppInfo != null ? mAppInfo.AppUrl : "";
    }

    public boolean hasAppForcedUpdate() {
        return mAppInfo != null && mAppInfo.ForceUpdate;
    }

    public boolean hasMultiLoggedIn() {
        return (mAppInfo != null) && "multilogin".equals(mAppInfo.UserStatus.toLowerCase());
    }

    public void setSyncId(String ticket) {
        mSharedPrefHelper.setData(Constants.API.KEY_TICKET, ticket);
    }

    public String getSyncId() {
        return mSharedPrefHelper.getString(Constants.API.KEY_TICKET, "");
    }

    public void logout() {
        setAppInfo(null);

        // 删除保存的用户信息
        mSharedPrefHelper.setData(Constants.API.KEY_TICKET, "");
    }

    public ProductShareInfo getmShareInfo() {
        return mShareInfo;
    }

    public void setmShareInfo(ProductShareInfo mShareInfo) {
        this.mShareInfo = mShareInfo;
    }
}
